package com.aster.service.manage.controller;

import com.aster.common.annotation.Log;
import com.aster.common.base.BaseController;
import com.aster.common.base.BaseDomain;
import com.aster.common.dto.UserToken;
import com.aster.common.exception.ServiceException;
import com.aster.common.utils.PageUtils;
import com.aster.common.utils.Query;
import com.aster.common.utils.R;
import com.aster.service.manage.domain.TbHospitalInfo;
import com.aster.service.manage.domain.TbLabelInfo;
import com.aster.service.manage.dto.DtoHospitalInfo;
import com.aster.service.manage.dto.do2dto.HospitalInfoConvert;
import com.aster.service.manage.dto.do2dto.LabelInfoConvert;
import com.aster.service.manage.service.HospitalInfoService;
import com.aster.service.manage.service.criteria.HospitalCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/hospital")
public class HospitalInfoController extends BaseController {

    @Autowired
    HospitalInfoService hospitalInfoService;

    @Log("用户列表")
    @GetMapping
    public R listByPage(@RequestParam Map<String, Object> params){
        Query query = new Query(params);
        HospitalCriteria criteria = new HospitalCriteria();
        criteria.setPageSize(query.getLimit());
        criteria.setCurrentPage(query.getPage());
        Map<String,Object> likeMap = new HashMap<>();
        likeMap.put("name",query.get("name"));
        criteria.setConditionLike(likeMap);
        Map<String,Object> map = new HashMap<>();
        map.put("deleted",1);
        criteria.setCondition(map);
        criteria.setOrderByClause("id");
        PageUtils pageUtils = hospitalInfoService.getHospitalInfoForPage(criteria);
        return R.ok().put("page",pageUtils);
    }
    @Log("根据id获取用户信息")
    @GetMapping("getById")
    public R getById(@RequestBody Integer id){
        try{
            if(id == null){
                return R.error(10001,"传入参数为空");
            }
            TbHospitalInfo info = hospitalInfoService.queryById(id);
            return R.ok().put("info",info);
        }catch (ServiceException ex){
            return R.error(ex.getMessage());
        }
        catch (Exception ex){
            return R.error(ex.getMessage());
        }
    }

    @Log("添加医院信息")
    @PostMapping("/add")
    public R add(@RequestBody DtoHospitalInfo dtoHospitalInfo, HttpServletRequest headers){
        try {
            UserToken userToken = getUserToken(headers);
            if(userToken == null){
                return R.error401();
            }
            TbHospitalInfo tbHospitalInfo = HospitalInfoConvert.MAPPER.addData(dtoHospitalInfo);
            hospitalInfoService.addHospitalInfo(tbHospitalInfo,userToken);
            return R.ok();
        }
        catch (ServiceException ex){
            return R.error(ex.getMessage());
        }
        catch (Exception ex){
            return R.error(ex.getMessage());
        }
    }

    @Log("修改医院信息状态")
    @PostMapping("/edit")
    public R editState(@RequestBody DtoHospitalInfo dtoHospitalInfo, HttpServletRequest headers){
        try {
            UserToken userToken = getUserToken(headers);
            TbHospitalInfo tbLabelInfo = HospitalInfoConvert.MAPPER.dto2do(dtoHospitalInfo);
            hospitalInfoService.editHospitalInfo(tbLabelInfo,userToken);
            return R.ok();
        }catch (ServiceException ex){
            return R.error(ex.getMessage());
        }
        catch (Exception ex){
            return R.error(ex.getMessage());
        }
    }

    @Log("删除医院信息")
    @PostMapping("/delete")
    public R delete(@RequestBody DtoHospitalInfo dtoHospitalInfo,HttpServletRequest headers){
        try{
            if(dtoHospitalInfo == null){
               return R.error(10001,"传入参数为空");
            }
            if(dtoHospitalInfo.getId() == 0){
                return R.error(10001,"传入参数为空");
            }
            UserToken userToken = getUserToken(headers);
            if(userToken == null){
                return R.error401();
            }
            dtoHospitalInfo.setDeleted(BaseDomain.DELETED.NO.key);
            TbHospitalInfo tbHospitalInfo = HospitalInfoConvert.MAPPER.updateDeleted(dtoHospitalInfo);
            hospitalInfoService.editHospitalInfo(tbHospitalInfo,userToken);
            return R.ok();
        }catch (ServiceException ex){
            return R.error(ex.getMessage());
        }
        catch (Exception ex){
            return R.error(ex.getMessage());
        }
    }


    @Log("修改医院数据状态")
    @PostMapping("/changeState")
    public R changeState(@RequestBody DtoHospitalInfo dtoHospitalInfo,HttpServletRequest headers){
        try {
            if(dtoHospitalInfo == null){
                return R.error(10001,"传入参数为空");
            }
            if(dtoHospitalInfo.getId() == 0){
                return R.error(10001,"传入参数为空");
            }
            UserToken userToken = getUserToken(headers);
            if(userToken == null){
                return R.error401();
            }
            TbHospitalInfo tbHospitalInfo = HospitalInfoConvert.MAPPER.updateState(dtoHospitalInfo);
            hospitalInfoService.editHospitalInfo(tbHospitalInfo,userToken);
            return R.ok();
        }catch (ServiceException ex){
            return R.error(ex.getMessage());
        }
        catch (Exception ex){
            return R.error(ex.getMessage());
        }
    }
}
